/**
 * Copyright 2018-2020 stylefeng & fengshuonan (https://gitee.com/stylefeng)
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.wgx.commons.config;

import com.google.common.collect.Ordering;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

/**
 * swagger配置类
 *
 */
@Configuration
@EnableSwagger2
@EnableWebMvc
public class SwaggerConfig {
    @Bean
    public Docket courseApi() {
        return createDocket("cn.wgx.module." ,"question" , "题库");
    }


    protected ApiInfo apiInfo(String module, String title) {
        ApiInfo apiInfo = new ApiInfo(
                "CG中心API",
                "这里是<span style='color:red;font-weight:bold;'> ["+title+"] </span>模块所有的接口，里边含有说明，仅供测试.返回结果全部为</br>" +
                        "{</br>'success': true/false,</br>" +
                        " 'data': 结果集,</br>" +
                        " 'result': '修改结果数量'</br>}</br>" +
                        " 'msg': '错误信息'</br>}</br>" +
                        "如有特殊情况会注明.</br>" +
                        "_startDate与_endDate为通用查询字段,所有实体都有,可判断时间范围: " +
                        "_startDate < createDate < _endDate" +
                        "目前带中文注释的为可用api.",
                "1.0",
                "",
                ApiInfo.DEFAULT_CONTACT,
                "",
                "",
                new ArrayList<VendorExtension>());
        return apiInfo;
    }


    protected Docket createDocket(String basepackage, String module, String name, String title){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName(name)
                .select()
                .apis(RequestHandlerSelectors.basePackage(basepackage + module))
                .build()
//                .operationOrdering((Operation left, Operation right) ->  ((left.getPosition() - right.getPosition()) > 0 ? 1:0))
                .operationOrdering(new Ordering<Operation>(){
                    @Override
                    public int compare(Operation left, Operation right) {
//                        return (left.getPosition() - right.getPosition()) > 0 ? 1:0;
//System.out.println(left.getSummary() + " - " + right.getSummary());
                        return left.getSummary().compareTo(right.getSummary());

                    }
                }).apiListingReferenceOrdering(new Ordering<ApiListingReference>(){
                    @Override
                    public int compare(ApiListingReference left, ApiListingReference right) {
                        int leftPos =  left.getPosition();
                        int rightPos = right.getPosition();
                        int position = Integer.compare(leftPos, rightPos);
                        if(position == 0) {
                            position = left.getPath().compareTo(right.getPath());
                        }
                        return position;
                    }
                }).apiDescriptionOrdering(new Ordering<ApiDescription>() {
                    @Override
                    public int compare(ApiDescription left, ApiDescription right) {
                        int leftPos = left.getOperations().size() == 1 ? left.getOperations().get(0).getPosition() : 0;
                        int rightPos = right.getOperations().size() == 1 ? right.getOperations().get(0).getPosition() : 0;
                        int position = Integer.compare(leftPos, rightPos);
                        if(position == 0) {
                            position = left.getPath().compareTo(right.getPath());
                        }
                        return position;
                    }
                })
                .apiInfo(apiInfo(name, title));
    }

    protected Docket createDocket(String basepackage, String module, String title){
        return createDocket(basepackage, module, module, title);
    }
}
